﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;

namespace DFGK_WMS.WebAPI.Controllers.权限管理
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "RBAC")]
    public class UserController : BaseController
    {
        private readonly IUserService userService; 
        private readonly TokenHelper tokenHelper;
        public UserController(IWebHostEnvironment environment, TokenHelper tokenHelper,IUserService userService) : base(environment)
        {
            this.tokenHelper = tokenHelper;
            this.userService = userService;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddUser(User m)
        {
            if (string.IsNullOrEmpty(m.UserName) || string.IsNullOrEmpty(m.RealName))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            string message=string.Empty;
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool result = userService.AddUser(m,out message);

            if (result) 
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！"+message, "200"));
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdUser(User m)
        {
            if (string.IsNullOrEmpty(m.UserName) || string.IsNullOrEmpty(m.RealName))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            string message = string.Empty;
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            bool result = userService.UpdUser(m, out message);

            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！" + message, "500"));
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelUser(long uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = userService.DelUser(uid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 判断登录
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public IActionResult GetLogin(LoginDto m)
        {
            if (m.UserName.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "账号不能为空！"));
            }
            if (m.UserPassword.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "密码不能为空！"));
            }
            string message = string.Empty;
            var obj = userService.GetLogin(m, out message);
            if(obj==null)
                return Ok(BaseResult.Result(null, message, "500"));

            var token = tokenHelper.CreateJwtToken(obj);
            obj.Token = token;
            return Ok(BaseResult.Result(obj, "登录成功", "200"));
        }

        /// <summary>
        /// 根据用户Id获取对应权限
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuitemAction(long uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }

            var list= userService.GetMenuitemAction(uid);
            if (list.Any())
            {
                return Ok(BaseResult.Result(list, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 根据用户Id获取对应权限（升级版）
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuitem(long uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }

            var list = userService.GetMenuitem(uid);
            if (list.Any())
            {
                return Ok(BaseResult.Result(list, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="uname">用户名</param>
        /// <param name="rid">角色Id</param>
        /// <param name="state">状态</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserList(string? uname = "", int rid = 0, int state = 3, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount = 0;
            var list = userService.GetUserList(uname, rid, state, pageIndex, pageSize, out totalCount);
            if (list!=null)
            {
                return Ok(BaseResult.Result(list, "获取成功", "200",totalCount));
            }
            return Ok(BaseResult.Result(null, "获取失败", "200",0));
        }

        /// <summary>
        /// 根据主键获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserById(long uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = userService.GetUserById(uid);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }
    }
}
